package vumt.radiocountries.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import vumt.radiocountries.R;
import vumt.radiocountries.object.ChannelObject;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/*
 * adapter cho list các country
 * */

public class CountryBaseAdapter extends BaseAdapter implements Filterable {
	private Context mContext;
	private ArrayList<ChannelObject> listCountry;
	private ArrayList<ChannelObject> origCountryList;
	private Filter countryFilter;

	DisplayImageOptions options;
	ImageLoader imageLoader;
	ImageLoadingListener animateFirstListener;

	public CountryBaseAdapter(Context c, ArrayList<ChannelObject> list) {
		this.mContext = c;
		this.listCountry = list;
		this.origCountryList = list;

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.blank_image)
				.showImageForEmptyUri(R.drawable.blank_image)
				.showImageOnFail(R.drawable.blank_image).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(20))
				.build();
		imageLoader = ImageLoader.getInstance();
		animateFirstListener = new AnimateFirstDisplayListener();
	}

	@Override
	public int getCount() {
		return listCountry.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = mInflater.inflate(R.layout.item_row_country, parent, false);
			ViewHolder holder = new ViewHolder();
			holder.imgThumbnail = (ImageView) v.findViewById(R.id.ivFlag);
			holder.name = (TextView) v.findViewById(R.id.tvName);
			holder.numStation = (TextView) v.findViewById(R.id.tvNumStation);
			v.setTag(holder);
		}
		ChannelObject channel = listCountry.get(position);
		if (channel != null) {
			ViewHolder holder = (ViewHolder) v.getTag();
			imageLoader.displayImage(channel.flag, holder.imgThumbnail,
					options, animateFirstListener);
			holder.name.setText(channel.country);
			holder.numStation.setText(channel.listSubChannel.size()
					+ " radio station");
		}
		return v;
	}

	public void resetData() {
		listCountry = origCountryList;
	}

	static class ViewHolder {
		ImageView imgThumbnail;
		TextView name;
		TextView numStation;
	}

	@Override
	public Filter getFilter() {
		if (countryFilter == null)
			countryFilter = new CountryFilter();
		return countryFilter;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

	private class CountryFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();

			if (constraint == null || constraint.length() == 0) {
				results.values = origCountryList;
				results.count = origCountryList.size();
			} else {
				ArrayList<ChannelObject> nCountryList = new ArrayList<ChannelObject>();

				for (ChannelObject c : listCountry) {
					if (c.getCountry().toUpperCase()
							.startsWith(constraint.toString().toUpperCase()))
						nCountryList.add(c);
				}
				results.values = nCountryList;
				results.count = nCountryList.size();
			}
			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			if (results.count == 0)
				notifyDataSetInvalidated();
			else {
				listCountry = (ArrayList<ChannelObject>) results.values;
				notifyDataSetChanged();
			}
		}
	}
}
